<template>
  <div>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="700px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
      <!--      <el-form-item label="类型" prop="lx">-->
      <!--        <el-select :clearable="true" v-model="dataForm.lx" placeholder="请选择类型" class="xiaLa" style="width:90%;">-->
      <!--          <el-option v-for="(item,index) in lxOptions " :key="index" :label="item.label" :value="item.label">-->
      <!--          </el-option>-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="人员名称" prop="userName">
        <!-- <el-select :clearable="true" v-model="dataForm.userCode" filterable placeholder="请选择" class="xiaLa" style="width:100%;">
          <el-option v-for="(item,index) in ryOptions " :key="index" :label="item.userName" :value="item.userCode">
          </el-option>
        </el-select> -->
        <el-input v-model="dataForm.userName" readonly  @focus="changeRen" placeholder="请选择人员"></el-input>
      </el-form-item>
      <el-form-item label="人员编号" prop="userCode">
        <el-input v-model="dataForm.userCode" readonly></el-input>
      </el-form-item>
       <!-- prop="jjxId" -->
      <el-form-item label="奖金项名称">
        <el-select :clearable="true" v-model="dataForm.jjxId" @change="changeJiang(dataForm.jjxId)" placeholder="请选择" class="xiaLa" style="width:100%;">
          <el-option v-for="(item,index) in jjxOptions " :key="index" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="仅输入金额" prop="sFje">
        <!-- <el-select :clearable="true" v-model="dataForm.sFje"  placeholder="请选择" class="xiaLa" style="width:100%;">
          <el-option   label="" value="1"></el-option>
          <el-option   label="" value="0"></el-option>
        </el-select> -->
        <el-radio-group v-model="dataForm.sFje" @change="isFou(dataForm.sFje)">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>>
        </el-radio-group>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="奖惩金额" prop="jjJe">
            <el-input-number v-model="dataForm.jjJe" :precision="2" :step="100" style="width:100%;" @change="changJine()"></el-input-number>
             <!-- @change="jeFzZh('je')" -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="奖惩分值" prop="jjFz">
            <el-input-number v-model="dataForm.jjFz" :precision="2" :step="1" :disabled="isdisabled" style="width:100%" @change="changFenz()"></el-input-number>
             <!-- @change="jeFzZh('fz')" -->
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="转换系数">
          <el-input-number v-model="jeFzXs" disabled  @change="jeFzZh('xs')" :precision="2" :step="1" :min="0" style="width: 150px"></el-input-number>
          <!-- <span style="color: #0E258C;margin-left: 10px">奖惩分值 × 转换系数 = 奖惩金额</span> -->
        </el-form-item>
      </el-row>
      <el-form-item label="奖惩时间" prop="jjDate">
        <el-date-picker v-model="dataForm.jjDate" placeholder="奖惩时间" style="width:100%;" type="datetime"
                        :picker-options="expireTimeOption" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="奖惩说明" prop="jjSm">
        <el-input v-model="dataForm.jjSm" placeholder="奖惩说明" :rows="3" type="textarea" style="width:100%;" show-word-limit maxlength="250"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
  <personCard v-if="showPersonCard" :multiple='false' ref="personCardRefs" @getUserList="getUserCheck"></personCard>
  </div>
</template>

<script>
import {getObj, addObj, putObj} from '@/api/jiangjin/jiangjingrjcjlb'
import {getRyfzmxbByJjDeptId} from '@/api/jiangjin/jiangjinryfzmxb'
import {getJjDeptJjx} from '@/api/jiangjin/jiangjinjjxxxb'
import personCard from './personCard'
export default {
  components:{
    personCard
  },
  data() {
    return {
      showPersonCard:false,
      dataValidate: '',
      visible: false,
      canSubmit: false,
      lxOptions: [{value: '奖励', label: '奖励'}, {value: '惩罚', label: '惩罚'}],
      ryOptions: [],
      jjxOptions: [],
      jeFzXs: 100,
      dataForm: {
        id: 0,
        jjDeptId: '',
        lx: '',
        userCode: '',
        userName: '',
        jjxId: '',
        jjxName: '',
        jjJe: '',
        jjFz: '',
        jjDate: '',
        jjSm: '',
        jjFfzt: '',
        jjFfid: '',
        createTime: '',
        editTime: '',
        createDeptId: '',
        createDeptName: '',
        createUserId: '',
        createUserName: '',
        editDeptId: '',
        editDeptName: '',
        editUserId: '',
        editUserName: '',
        dataState: '',
        deptId: '',
        tenantId: '',
        sFje:0
      },
      isdisabled:false,
      dataRule: {
        lx: [
          {required: true, message: '类型不能为空', trigger: 'blur'}
        ],
        userCode: [
          {required: true, message: '人员名称不能为空', trigger: 'blur'}
        ],
        jjxId: [
          {required: true, message: '请选择奖金项名称', trigger: 'blur'}
        ],
        sFje:[
           {required: true, message: '请选择是否只填写金额', trigger: 'blur'}
        ],
        //  jjJe: [
        //   {required: true, message: '奖惩金额不能为空', trigger: 'blur'}
          // ,{
          //   validator: function (rule, value, callback) {
          //     if (value) {
          //       callback();
          //     } else {
          //       let tip = "";
          //       if (value * 1 === 0) {
          //         tip = "奖惩金额应大于0"
          //       }
          //       if (tip) {
          //         callback(new Error(tip));
          //       } else {
          //         callback();
          //       }
          //     }
          //   }, trigger: "blur"
          // }
       // ],
        // jjFz: [{required: true, message: '奖惩分值不能为空', trigger: 'blur'}],
        jjDate: [
          {required: true, message: '奖惩时间不能为空', trigger: 'blur'}
        ],
      },
      expireTimeOption: {
        disabledDate(date) {
          //disabledDate 文档上：设置禁用状态，参数为当前日期，要求返回 Boolean
          return (
            date.getTime() < new Date(new Date().getFullYear(), 0, 1).getTime() ||
            date.getTime() > new Date(new Date().getFullYear(), 11, 31).getTime()
          );
        }
      },
      jiangXlist:[],
      fzType:0
    }
  },
  methods: {
    init(id, deptId) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.jeFzXs = 100;
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data.data;
            console.log("this.dataForm:"+JSON.stringify(this.dataForm))
            if (this.dataForm.lx.trim() !== "奖励") {
              this.dataForm.jjJe = -this.dataForm.jjJe;
              this.dataForm.jjFz = -this.dataForm.jjFz;
            }
          })
        } else {
          this.$set(this.dataForm, "jjDeptId", deptId);
          this.$set(this.dataForm, "jjFfzt", "未发放");
          console.log("this.dataForm:"+JSON.stringify(this.dataForm))
        }
      })

      getRyfzmxbByJjDeptId(deptId).then(res => {
        const xData = res.data.data;
        this.ryOptions = [];
        xData.map(item => {
          this.ryOptions.push({
            userCode: item.ryCode.toString(),
            userName: item.ryName
          })
        })
      })

      getJjDeptJjx(deptId).then(res => {
        this.jiangXlist = res.data.data;
        this.jjxOptions = [];
        this.jiangXlist.map(item => {
          this.jjxOptions.push({
            value: item.id,
            label: item.jjxMc
          })
        })
      })
    },
    isFou(val){
     // console.log(val)
      if(val == 1){
        this.isdisabled = true;
        this.dataForm.jjFz = "";
      }else{
        this.isdisabled = false;
      }
    },
    //金额改变
    changJine(){
      let jin=0;
      if(this.dataForm.sFje==0&&this.jeFzXs!=0){
        jin=this.dataForm.jjJe*1/this.jeFzXs*1
        this.$set(this.dataForm, "jjFz", jin);
        // if(this.jeFzXs*1>0){
        //   if(this.fzType==1){
        //     jin=this.dataForm.jjJe*1/this.jeFzXs*1
        //     this.$set(this.dataForm, "jjFz", jin);
        //   }
        //   if(this.fzType==2){
        //     jin=this.dataForm.jjJe*1*this.jeFzXs*1/100
        //     this.$set(this.dataForm, "jjFz", jin);
        //   }
        // }
      }
      
    },
    //分值改变
    changFenz(){
      let  fen=0;
      fen=this.dataForm.jjFz*1*this.jeFzXs*1
      this.$set(this.dataForm, "jjJe", fen);
      // if(this.jeFzXs*1>0){
      //   if(this.fzType==1){
      //     fen=this.dataForm.jjFz*1*this.jeFzXs*1
      //     this.$set(this.dataForm, "jjJe", fen);
      //   }
      //   if(this.fzType==2){
      //     fen=this.dataForm.jjFz*1*100/this.jeFzXs*1
      //     this.$set(this.dataForm, "jjJe", fen);
      //   }
      // }
    },
    //选择奖金项
    changeJiang(jjxId){
      const list=this.jiangXlist.filter(item=>item.id==jjxId)
      console.log(JSON.stringify(list))

      this.$set(this.dataForm, "jjJe", 0);
      this.$set(this.dataForm, "jjFz", 0);
      this.jeFzXs=list[0].jeFzXs
      // if(list[0].jjFz*1>0){
      //   this.jeFzXs=list[0].jjFz;
      //   this.fzType=1
      // }
      // if(list[0].jjFz*1==0&&list[0].jjJe*1>0){
      //   this.jeFzXs=list[0].jjJe
      //   this.fzType=2
      // }
      // if(list[0].jjFz*1==0&&list[0].jjJe*1==0){
      //   this.jeFzXs=0
      // }
    },
    //选择人员
    changeRen(){
      var data={
        enter:"单个新增"
      }
      this.showPersonCard=true;
        this.$nextTick(()=>{
          this.$refs.personCardRefs.init(data)
        })
      },
    //取得选择的人员信息
    getUserCheck(data){
      if(data.length === 0){
        this.dataForm.userCode = "";
        this.dataForm.userName = "";
      }else {
        const c1= this.getStrJoin("ryXm",data);
        const c2= this.getStrJoin("ryCode",data);
        this.dataForm.userCode =  c2.join(',');
        this.dataForm.userName = c1.join(',');
      }
    },
    //拼接字符串
    getStrJoin(strKey,strData){
      let lTemp = [];
      strData.map(item => {
        lTemp.push(item[strKey]);
      })
      return lTemp;
    },
    getRyUserName(value) {
      let lResult = "";
      console.log(value)
      console.log(JSON.stringify(this.ryOptions))
      for (var i = 0; i < this.ryOptions.length; i++) {
        if (this.ryOptions[i].userCode == value) {
          lResult = this.ryOptions[i].userName;
          break;
        }
      }
      console.log(lResult)
      return lResult;
    },
    getJjxName(value) {
      let lResult = "";
      this.jjxOptions.find(item => {
        if (item.value === value) {
          lResult = item.label;
        }
      })
      return lResult;
    },
    //奖惩金额分值转换
    jeFzZh(type) {
      if (type == 'je' && this.jeFzXs != 0) {//奖惩金额值改变触发
        this.dataForm.jjFz = this.dataForm.jjJe / this.jeFzXs;
      } else if (type == 'fz' || type == 'xs') {//分值|系数改变触发
        this.dataForm.jjJe = this.dataForm.jjFz * this.jeFzXs;
      }
    },
    // 表单提交
    dataFormSubmit() {
      let ljjJe = 0;
      let ljjFz = 0;
      
     
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;

         // this.$set(this.dataForm, "userName", this.getRyUserName(this.dataForm["userCode"].toString()));  //不去保存时候会重置名称
          this.$set(this.dataForm, "jjxName", this.getJjxName(this.dataForm.jjxId));
          console.log(JSON.stringify(this.dataForm))
          ljjJe = this.dataForm.jjJe;
          ljjFz = this.dataForm.jjFz;
          if (ljjJe >= 0) {
            this.dataForm.lx = "奖励";
          } else {
            this.dataForm.lx = "惩罚";
          }
          this.dataForm.jjJe = Math.abs(ljjJe);
          this.dataForm.jjFz = Math.abs(ljjFz);

          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.closeAll();
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    }
  }
}
</script>
